package com.online.dao.user.Impl;

import com.online.dao.user.merchantDao;

import com.online.pojo.Bill;
import com.online.pojo.menu;
import com.online.pojo.merchant;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class merchantImpl implements merchantDao {
    public merchant select(String name) throws SQLException {
        merchant shop=new merchant();
        ResultSet rs;
        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.merchant where shop_name=?;";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,name);
        rs= pstm.executeQuery();
        if (rs.next()) {
            shop.setShop_name(rs.getString(1));
            shop.setUser_name(rs.getString(2));
            shop.setPwd(rs.getString(3));
            shop.setLegal_p(rs.getString(4));
            shop.setTel(rs.getString(5));
            shop.setAccount(rs.getDouble(6));
        }
        System.out.println(shop);
        return shop;
    }


    public List<menu> select(List<menu> list,String shop_name) throws SQLException {
        ResultSet rs;
        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.menu where shop_name=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,shop_name);
        rs = pstm.executeQuery();
        int i=0;
        while (rs.next()){
            menu menu = new menu();
            menu.setId(rs.getInt(1));
            menu.setName(rs.getString(2));
            menu.setShop_name(rs.getString(3));
            menu.setPrice(rs.getDouble(4));
            menu.setType(rs.getString(5));
            list.add(i,menu);
            i++;

        }
        System.out.println(list);
        return list;
    }



    public boolean insert(String name, String shop_name, double price, String type) {
        int rs;
        boolean falg = false;
        try {
            Connection connection = BaseDao.getConnection();
            String sql = "insert into online.menu(name, shop_name, price, type) VALUES (?,?,?,?)";
            PreparedStatement pstm = connection.prepareStatement(sql);
            pstm.setString(1,name );
            pstm.setString(2,shop_name );
            pstm.setDouble(3, price);
            pstm.setString(4, type);

            rs = pstm.executeUpdate();
            if (rs != 0) {
                falg = true;
                System.out.println("提交审批成功");
            } else {
                falg = false;
                System.out.println("提交审批失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return falg;
    }

    public boolean del(int id) throws SQLException {
        boolean falg=false;
        int rs;
        Connection connection = BaseDao.getConnection();
        String sql ="delete from online.menu where id=?;";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setInt(1,id);
        rs=pstm.executeUpdate();
        if (rs>0){
            System.out.println("已删除");
            falg=true;
        }
        return falg;
    }

    public List<menu> select(List<menu> list, double price, String type,String name,String shop_name) throws SQLException {
        ResultSet rs;
        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.menu where price<=? and type=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setDouble(1,price);
        pstm.setString(2,type);
        rs = pstm.executeQuery();
        int i=0;
        while (rs.next()){
            menu menu = new menu();
            menu.setId(rs.getInt(1));
            menu.setName(rs.getString(2));
            menu.setShop_name(rs.getString(3));
            menu.setPrice(rs.getDouble(4));
            menu.setType(rs.getString(5));

            list.add(i,menu);
            i++;

        }
        System.out.println(list);
        return list;
    }

    public menu select(menu menu, int id) throws SQLException {
        menu menu1=new menu();
        ResultSet rs;
        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.menu where id=?;";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setInt(1,id);
        rs= pstm.executeQuery();
        if (rs.next()) {
           menu.setId(rs.getInt(1));
            menu.setName(rs.getString(2));
            menu.setShop_name(rs.getString(3));
            menu.setPrice(rs.getDouble(4));
            menu.setType(rs.getString(5));
        }
        return menu1;
    }

    public boolean insert(String shop_name, int shopid, String user_name, double price, int pay) {
        int rs;
        boolean falg = false;
        try {
            Connection connection = BaseDao.getConnection();
            String sql = "insert into online.bill(shop_name, shopid, user_name, price, pay) VALUES (?,?,?,?,?)";
            PreparedStatement pstm = connection.prepareStatement(sql);
            pstm.setString(1,shop_name );
            pstm.setInt(2,shopid );
            pstm.setString(3, user_name);
            pstm.setDouble(4, price);
            pstm.setInt(5, pay);
            rs = pstm.executeUpdate();
            if (rs != 0) {
                falg = true;
                System.out.println("提交订单成功");
            } else {
                falg = false;
                System.out.println("提交订单失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return falg;
    }

    public boolean update(Bill bill) throws SQLException {
        boolean falg=false;
        int rs;
        Connection connection = BaseDao.getConnection();
        String sql = "update online.merchant set account=account+? where shop_name=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setDouble(1,bill.getPrice());
        pstm.setString(2,bill.getShop_name());


        rs=pstm.executeUpdate();
        if(rs>0) {
            falg=true;
            System.out.println("更新成功");
        }
        return falg;
    }

    public merchant selectUsername(String name) throws SQLException {
        merchant shop=new merchant();
        ResultSet rs;
        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.merchant where user_name  =?;";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,name);
        rs= pstm.executeQuery();
        if (rs.next()) {
            shop.setShop_name(rs.getString(1));
            shop.setUser_name(rs.getString(2));
            shop.setPwd(rs.getString(3));
            shop.setLegal_p(rs.getString(4));
            shop.setTel(rs.getString(5));
            shop.setAccount(rs.getDouble(6));
        }
        System.out.println(shop);
        return shop;
    }
}
